package com.trdcore.zyzkt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trdcore.zyzkt.mapper.CourseMapper;
import com.trdcore.zyzkt.model.vod.Course;
import com.trdcore.zyzkt.model.vod.CourseDescription;
import com.trdcore.zyzkt.model.vod.Subject;
import com.trdcore.zyzkt.model.vod.Teacher;
import com.trdcore.zyzkt.service.*;
import com.trdcore.zyzkt.vo.vod.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Luffy
 * @since 2024-03-18
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {


    @Autowired
    private TeacherService teacherService;

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private CourseDescriptionService descriptionService;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private VideoService videoService;

    @Autowired
    private ChapterService chapterService;
    @Override
    public Map<String, Object> findPage(Page<Course> pageParam, CourseQueryVo courseQueryVo) {

        String title = courseQueryVo.getTitle();
        Long subjectId = courseQueryVo.getSubjectId();//二层分类
        Long subjectParentId = courseQueryVo.getSubjectParentId();//一层分类
        Long teacherId = courseQueryVo.getTeacherId();

        QueryWrapper<Course> wrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if(!(subjectId == null)){
            wrapper.like("subject_id",subjectId);
        }
        if(!(subjectParentId == null)){
            wrapper.like("subject_parent_id",subjectParentId);
        }
        if(!(teacherId == null)){
            wrapper.like("teacher_id",teacherId);
        }

        Page<Course> pages = baseMapper.selectPage(pageParam, wrapper);
        long totalCount = pages.getTotal();
        long totalPage = pages.getPages();
        List<Course> list = pages.getRecords();
        list.forEach(this::getNameById);
        Map<String,Object> map=new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("totalPage",totalPage);
        map.put("records",list);
        return map;
    }

    @Override
    public Long saveCourse(CourseFormVo courseFormVo) {

        Course course=new Course();
        BeanUtils.copyProperties(courseFormVo,course);
        baseMapper.insert(course);

        CourseDescription courseDescription=new CourseDescription();
        courseDescription.setDescription(courseFormVo.getDescription());
        courseDescription.setCourseId(course.getId());
        descriptionService.save(courseDescription);
        return course.getId();
    }

    @Override
    public CourseFormVo getCourseInfoById(Long id) {
        Course course = baseMapper.selectById(id);
        if(course ==null){
            return null;
        }
        //CourseDescription courseDescription = descriptionService.getById(course.getId());
        QueryWrapper<CourseDescription> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id", course.getId());
        CourseDescription courseDescription = descriptionService.getOne(wrapper);
        CourseFormVo courseFormVo=new CourseFormVo();
        BeanUtils.copyProperties(course,courseFormVo);
        if(courseDescription!=null){
            courseFormVo.setDescription(courseDescription.getDescription());
        }
        return courseFormVo;
    }

    @Override
    public void updateCourseId(CourseFormVo courseFormVo) {

        Course course=new Course();
        BeanUtils.copyProperties(courseFormVo,course);
        baseMapper.updateById(course);

        CourseDescription courseDescription=new CourseDescription();
        courseDescription.setDescription(courseFormVo.getDescription());
        courseDescription.setId(course.getId());
        descriptionService.updateById(courseDescription);

    }

    @Override
    public CoursePublishVo getCoursePublishVo(Long id) {
        return courseMapper.selectCoursePublishVoById(id);
    }

    //根据id发布课程
    @Override
    public boolean publishCourseById(Long id) {
        Course course = new Course();
        course.setId(id);
        course.setPublishTime(new Date());
        course.setStatus(1);
        return this.updateById(course);
    }

    @Override
    public void removeCourseById(Long id) throws Exception {
        //根据课程id删除小节
        videoService.removeVideoByCourseId(id);
        //根据课程id删除章节
        chapterService.removeChapterByCourseId(id);
        //根据课程id删除描述
        descriptionService.removeById(id);
        //根据课程id删除课程
       baseMapper.deleteById(id);
    }

    @Override
    public Map<String, Object> getInfoById(Long courseId) {
            //更新流量量
            Course course = baseMapper.selectById(courseId);
            course.setViewCount(course.getViewCount() + 1);
            baseMapper.updateById(course);

            Map<String, Object> map = new HashMap<>();
            CourseVo courseVo = baseMapper.selectCourseVoById(courseId);
            List<ChapterVo> chapterVoList = chapterService.getTreeList(courseId);
            CourseDescription courseDescription = descriptionService.getById(courseId);
            Teacher teacher = teacherService.getById(course.getTeacherId());

            //TODO后续完善
            Boolean isBuy = false;

            map.put("courseVo", courseVo);
            map.put("chapterVoList", chapterVoList);
            map.put("description", null != courseDescription ?
                    courseDescription.getDescription() : "");
            map.put("teacher", teacher);
            map.put("isBuy", isBuy);//是否购买
            return map;
    }

    @Override
    public List<Course> findlist() {
        List<Course> list = baseMapper.selectList(null);
        list.stream().forEach(item -> {
            this.getNameById(item);
        });
        return list;
    }

    private Course getNameById(Course course) {
        Teacher teacher = teacherService.getById(course.getTeacherId());
        if(teacher!=null){
            String name = teacher.getName();
            course.getParam().put("teacherName",name);
        }
        Subject subjectOne = subjectService.getById(course.getSubjectParentId());
        if(subjectOne!=null){
            course.getParam().put("subjectParenTitle",subjectOne.getTitle());
        }
        Subject subjectTwo = subjectService.getById(course.getSubjectId());
        if(subjectTwo!=null){
            course.getParam().put("subjectTitle",subjectTwo.getTitle());
        }
        return course;
    }
}
